/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import edu.upc.pe.Students;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

/**
 *
 * @author GROSSEL
 */
@Stateless
@Path("edu.upc.pe.students")
public class StudentsFacadeREST extends AbstractFacade<Students> {
    @PersistenceContext(unitName = "PC2_GerardoRosselPU")
    private EntityManager em;

    public StudentsFacadeREST() {
        super(Students.class);
    }

    @POST
    @Path("POST/students")
    @Override
    @Consumes({"application/xml", "application/json"})
    public void create(Students entity) {
        super.create(entity);
    }

    @DELETE
    @Path("DELETE/students/{id}")
    public void remove(@PathParam("id") String id) {
        super.remove(super.find(id));
    }
   
    @GET
    @Path("GET/students/{id}")
    @Produces({"application/xml", "application/json"})
    public Students find(@PathParam("id") String id) {
        return super.find(id);
    }

    @GET
    @Path("GET /students")
    @Produces({"application/xml", "application/json"})
    public List<Students> findAll() {
        return super.findAll();
    }

    @PUT
    @Path("PUT/students/{id}")
    @Consumes({"application/xml", "application/json"})
    public void editStudents(Students entity) {
        super.edit(entity);
    }
   
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
    @GET
    @Path("GET /students/search/{lastName}/{firstName}")
    @Produces({"application/xml", "application/json"})
    public List<Students> consulta1(@PathParam("lastName") String lastName, @PathParam("firstName") String firstName) {
        Query q = em.createQuery("SELECT s FROM Students s WHERE s.firstName = :firstName AND s.lastName = :lastName");
        q.setParameter("lastName", lastName);
        q.setParameter("firstName", firstName);
        return q.getResultList();
    }
    
}
